package cn.lyq.card.ui.address

import android.graphics.Color
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseDialogFg
import cn.lyq.card.data.model.AreaInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.global.BusTag
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.dialog_area_select.*


class AreaSelectFg : BaseDialogFg() {

    private val mModel by lazy {
        getVM(AddressVM::class.java)
    }

    override val rootId: Int
        get() = R.layout.dialog_area_select

    override fun initView(view: View) {
        super.initView(view)
        arrayOf(wheel1, wheel2, wheel3).forEach {
            it.isCurved = false
            it.textSize = resources.getDimensionPixelOffset(R.dimen.font17).toFloat()
            it.isDrawSelectedRect = true
            it.selectedRectColor = Color.parseColor("#F0F4FC")
            it.setSelectedItemTextColorRes(R.color.font_blue)
            it.setNormalItemTextColorRes(R.color.font_gray)
            it.lineSpacing = resources.getDimensionPixelOffset(R.dimen.font17).toFloat()
            it.setOnItemSelectedListener { wheelView, _, position ->
                val area = wheelView.data[position]
                if (area is AreaInfo && wheelView.id != R.id.wheel3) {
                    getArea(area.id, wheelView.id == R.id.wheel1)
                }
            }
        }
    }

    override fun initObserver() {
        super.initObserver()
        mModel.getRegions("").observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> showLoading()
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
                Status.SUCCESS -> {
                    wheel1.data = it.requireData()
                    getArea(it.requireData()[0].id, true)
                }
            }
        })
        tvConfirm.setClick {
            val areaInfo = wheel1.selectedItemData as AreaInfo?
            val areaInfo1 = wheel2.selectedItemData as AreaInfo?
            val areaInfo2 = wheel3.selectedItemData as AreaInfo?
            if (areaInfo == null || areaInfo1 == null || areaInfo2 == null) {
                Toast.makeText(requireContext(), R.string.str_area_error_tip, Toast.LENGTH_SHORT)
                    .show()
                return@setClick
            }
            val areaStr = "${areaInfo.name}${areaInfo1.name}${areaInfo2.name}"
            BusUtils.post(BusTag.AREA_SELECT, areaStr)
            dismiss()
        }
        tvCancel.setClick {
            dismiss()
        }
    }

    private fun getArea(id: Int, isCity: Boolean) {
        mModel.getRegions(id.toString()).observe(this, Observer {
            when (it?.status) {
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
                Status.SUCCESS -> {
                    if (isCity) {
                        wheel2.data = it.requireData()
                        wheel2.setSelectedItemPosition(0, false)
                        if (it.requireData().size > 0) {
                            getArea(it.requireData()[0].id, false)
                        } else {
                            wheel3.data = mutableListOf()
                        }
                    } else {
                        wheel3.data = it.requireData()
                        dismissLoading()
                    }
                }
            }
        })
    }
}